package com.vgemv.jsutilitysdkdemo.module.List.WordLive;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestDictionaryCallback;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewRefreshCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class WordLiveActivity extends AppCompatActivity {

    private RelativeLayout mrl_root;
    private ArrayList<JsMenuItemVm> mal_wordLive = new ArrayList<>();
    private static final String DATEFORMAT_default = "yyyy-MM-dd HH:mm:ss";
    private JsMenuFragment menuFragment_wordLive;
    private String lastWordLiveId = "";
    private String lastChatId = "";
    private String lastExtendId = "";
    private String pageSize = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JsRequestManager.sharedInstance().serverAddress = "http://1.14.158.158";
        setupUi();

    }

    //检查所有数据的日期,当前第一个显示,其余不显示.
    private void tagWordLiveArrayDate() {
        String currentDate = null;
        for(int i = 0;i<mal_wordLive.size();i++) {
            WordLiveViewVm vm = (WordLiveViewVm)mal_wordLive.get(i);
            if(i == 0) {
                currentDate = vm.model.publishDate;
                vm.mb_isShowDate = true;
            }
            else {
                if(vm.model.publishDate.equals(currentDate)) {
                    vm.mb_isShowDate = false;
                }
                else {
                    vm.mb_isShowDate = true;
                    currentDate = vm.model.publishDate;
                }
            }
        }
    }



    public void setupUi() {
        {
            //root view
            mrl_root = new RelativeLayout(this);
            mrl_root.setId(View.generateViewId());
            setContentView(mrl_root);
        }
        {
            //WordLive
            JsMenuFragment fragment = new JsMenuFragment();
            menuFragment_wordLive = fragment;
            fragment.moveToParentView(this, mrl_root);

            final int padding = WordLiveViewVm.itemViewParentHPadding;

            fragment.mBasicCallback = new JsMenuViewBasicCallback() {
                @Override
                public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                    return null;
                }

                @Override
                public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

                }

                @Override
                public Rect jsMenuViewSetItemOffset(final JsMenuView fragment, int index) {
                    Rect rect = new Rect();
                    if(index == 0) {
                        rect.top = padding;
                        rect.left = padding;
                        rect.right = padding;
                        rect.bottom = 0;
                    }
                    else if(index == fragment.vms().size() - 1) {
                        rect.top = padding;
                        rect.left = padding;
                        rect.right = padding;
                        rect.bottom = padding;
                    }
                    else {
                        rect.top = padding;
                        rect.left = padding;
                        rect.right = padding;
                        rect.bottom = 0;
                    }
                    return rect;
                }

                @Override
                public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                    JsMenuViewVm vm = new JsMenuViewVm();
                    vm.mi_layoutStyle = 2;
                    vm.mColor_background = Color.argb(255,200,200,200);
                    vm.mb_enableRefreshAndLoadmore = true;
                    return vm;
                }
                @Override
                public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                    WordLiveView chatItemView = new WordLiveView(context);
                    return chatItemView;
                }

                @Override
                public void jsMenuViewAfterCreateView(JsMenuView fragment) {

                }

                @Override
                public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                    return mal_wordLive;
                }

                @Override
                public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm model) {
                    JsLogManager.logc_d("MenuActivity/jsMenuViewClickItem","index is %d",i);
                }
            };
            fragment.mRefreshCallback = new JsMenuViewRefreshCallback() {
                @Override
                public void onRefresh(final JsMenuView fragment) {
                    loadFirstPageWordLive();

                }

                @Override
                public void onLoadMore(final JsMenuView fragment, int loadedPage) {
                    loadMorePageWordLive();
                }
            };
        }
        loadFirstPageWordLive();
    }

    public void loadFirstPageWordLive() {

        loadWordLiveContents("27", pageSize, "0","0", new JsRequestDictionaryCallback() {
            @Override
            public void onRequestResponse(int responseCode, Map resultObject, String detailInfo) {
                JsLogManager.logc_d("WordLiveActivity/onCreate",detailInfo);
                if(resultObject != null) {
                    mal_wordLive.clear();
                    ArrayList<HashMap> arrayList = (ArrayList) resultObject.get("list");
                    for (Map<String,Object> map : arrayList) {
                        WordLiveModel model = new WordLiveModel(map);
                        WordLiveViewVm vm = new WordLiveViewVm();

                        vm.model = model;
                        mal_wordLive.add(vm);
                    }
                    WordLiveViewVm lastVm = (WordLiveViewVm)mal_wordLive.get(mal_wordLive.size()-1);
                    lastWordLiveId = lastVm.model.id.toString();
                    tagWordLiveArrayDate();
                    menuFragment_wordLive.reloadData();
                    menuFragment_wordLive.finishRefresh(true,arrayList.size()>0);

                }
                else {
                    menuFragment_wordLive.finishRefresh(false,false);
                }
            }
        });
    }

    public void loadMorePageWordLive() {
        loadWordLiveContents("27", pageSize, lastWordLiveId,"0", new JsRequestDictionaryCallback() {
            @Override
            public void onRequestResponse(int responseCode, Map resultObject, String detailInfo) {
                JsLogManager.logc_d("WordLiveActivity/onCreate",detailInfo);
                if(resultObject != null) {
                    ArrayList<HashMap> arrayList = (ArrayList) resultObject.get("list");
                    for (int i = 0; i < arrayList.size(); i++) {
                        HashMap map = (HashMap) arrayList.get(i);
                        WordLiveModel model = new WordLiveModel(map);
                        WordLiveViewVm vm = new WordLiveViewVm();

                        vm.model = model;
                        mal_wordLive.add(vm);
                    }

                    WordLiveViewVm lastVm = (WordLiveViewVm)mal_wordLive.get(mal_wordLive.size()-1);
                    lastWordLiveId = lastVm.model.id.toString();

                    tagWordLiveArrayDate();
                    menuFragment_wordLive.reloadData();
                    menuFragment_wordLive.finishRefresh(true,arrayList.size()>0);

                }
                else {
                    menuFragment_wordLive.finishRefresh(false,false);
                }

            }
        });
    }
    public static void loadWordLiveContents(String roomId,String pageSize,String nextPageId,String order, JsRequestDictionaryCallback callback ) {
        String urlString = String.format("%s/%s?roomId=%s", JsRequestManager.sharedInstance().serverAddress,"apiserver/listWordLiveContent.action",roomId);
        HashMap map_param = new HashMap();
        map_param.put("roomID",roomId);
        map_param.put("nextPageID",nextPageId);
        map_param.put("pagesize",pageSize);
        map_param.put("order",order);
        JsRequestManager.sharedInstance().postReqeustByJson(urlString, map_param,callback);
    }
}